package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class DodajPracownikaInputFrame extends JFrame implements ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1622571017226747873L;

	JLabel l;
	JTextField text1, text2, text3, text4, text5;
	JButton button1, button2;
	JFormattedTextField text6;
	String n, i, em, t, s, z;
	Float d;
	DateFormat dF;

	
	public DodajPracownikaInputFrame() throws HeadlessException {
		super("Dodaj pracownika");
	}
	
	public void start()
	
    {
		JLabel label1 = new JLabel("Nazwisko: ");
	    JLabel label2 = new JLabel("Imie:     ");
	    JLabel label3 = new JLabel("E-mail:   ");
	    JLabel label4 = new JLabel("Telefon:  ");
	    JLabel label5 = new JLabel("Stanowisko:");
	    JLabel label6 = new JLabel("Data zatrudnienia:");


	    text1 = new JTextField(20);
	    text2 = new JTextField(20);
	    text3 = new JTextField(20);
	    text4 = new JTextField(20);
	    text5 = new JTextField(20);
	    dF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    text6 = new JFormattedTextField(dF);

	    button1 = new JButton("Dodaj");
	    button2 = new JButton("Zamknij");

	    this.add(label1, BorderLayout.WEST);
	    this.add(text1, BorderLayout.EAST);
	    this.add(label2, BorderLayout.WEST);
	    this.add(text2, BorderLayout.EAST);
	    this.add(label3, BorderLayout.WEST);
	    this.add(text3, BorderLayout.EAST);
	    this.add(label4, BorderLayout.WEST);
	    this.add(text4, BorderLayout.EAST);
	    
	    this.add(label6, BorderLayout.WEST);
	    text6.setText(dF.format(Calendar.getInstance().getTime()));
	    this.add(text6, BorderLayout.EAST);
	    this.add(label5, BorderLayout.WEST);
	    this.add(text5, BorderLayout.EAST);
	    this.add(button1);
	    this.add(button2);
	    
	    button1.addActionListener(this );
	    button2.addActionListener(this );
		
	    setLayout(new FlowLayout());
	    setSize(320,230);
	    setVisible(true);
	    Toolkit kit = Toolkit.getDefaultToolkit();
	    Dimension screenSize = kit.getScreenSize();
	    setLocation((screenSize.width-320)/2, (screenSize.height-230)/2);
	    setDefaultCloseOperation(HIDE_ON_CLOSE);

    }

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand() == "Zamknij"){
			this.dispose();
		}
		else if(e.getActionCommand() == "Dodaj"){
			try {
			     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			     Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=ABSI", "Admin","Admin");
			     CallableStatement statement = conn.prepareCall("EXEC DodajPracownika ?, ?, ?, ?, ?, ?"); 
			     	n = text1.getText();
			        i = text2.getText();
			        em = text3.getText();
			        t = text4.getText();
			        s = text5.getText();
			        z = text6.getText();
				     java.sql.Date cD = new java.sql.Date((dF.parse(z)).getTime());
			     
			     statement.setString(1,n);
			     System.out.println(n);
			     statement.setString(2,i);
			     statement.setString(3,t);
			     statement.setString(4,em);
			     statement.setString(5,s);
			     statement.setDate(6,(java.sql.Date) cD);
			     statement.execute();

			     conn.close();
			  } catch (Exception ex) {
			     ex.printStackTrace();
			  }
		}
		text1.setText("");
		text2.setText("");
		text3.setText("");
		text4.setText("");
		text5.setText("");
		
	}

}
